我應該使用什麼-String 或 StringBuilder 將 SQL 查詢存儲在使用許多不同 SQL 查詢的代碼中 (what should i use-String or StringBuilder for storing SQL queries in a code which uses many different SQL queries)


問題描述

我應該使用什麼‑String 或 StringBuilder 將 SQL 查詢存儲在使用許多不同 SQL 查詢的代碼中 (what should i use‑String or StringBuilder for storing SQL queries in a code which uses many different SQL queries)

我有一個使用多個 SQL 查詢的代碼。我應該使用 String 來存儲這些不同的 SQL 查詢還是應該使用 StringBuilder。

如果使用 StringBuilder,我應該將每個查詢放在一個新的 StringBuilder 對像中還是使用單個 StringBuilder 對象。


參考解法

方法 1:

String is Immutable and StringBuilder is Mutable i.e. no new object is created when you edit StringBuilder unlike String.

If your application is used in large scale then its advisable to use StringBuilder instead of String

NOTE:‑ String is Thread Safe while StringBuilder is not

方法 2:

Well, if you are willing concatenate theses queries into one query part by part, then use unique StringBuilder object instead of concatenating String objects to each other. That's the best practise in terms of performance.

Avoid using unique StringBuilder for all (different) concatenation sets in your class, that thing must be dangerous if you're using threads. You have to define new StringBuilder object for each set of concatenated strings.

(by kiranVisky GHatem)

參考文件

  1. what should i use‑String or StringBuilder for storing SQL queries in a code which uses many different SQL queries (CC BY‑SA 2.5/3.0/4.0)

#java #string #stringbuilder






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論